package com.example.service;

import com.example.entity.Devices;
import com.example.entity.Fan;
import com.example.entity.Params;
import com.example.repository.FanMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class FanService implements DeviceScheduleThreshold, DeviceValueUpdate {

    public static List<Integer> STATUS_FANS_LIST;

    static {
        STATUS_FANS_LIST = Lists.newArrayList(0);
    }

    @Resource
    private FanMapper fanMapper;

    @Resource
    private TemperatureHumiditySensorService temperatureHumiditySensorService;


    public PageInfo<Fan> findFan(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Fan> search = fanMapper.findFan(params);
        return PageInfo.of(search);
    }

    public void addFan(Fan fan) {
        fanMapper.insert(fan);
    }

    public void deleteFan(String Fid) {
        fanMapper.deleteById(Fid);
    }


    @Override
    public boolean threshold(Devices devices) {
        return Objects.nonNull(devices.getThreshold())
                && (temperatureHumiditySensorService.getTemperature() > devices.getThreshold()
        );
    }

    @Override
//    @AutoLog("操作空调")
    public void statusUpdate(String status) {
        if (StringUtils.equals(status, "1")) {
            STATUS_FANS_LIST.set(0, 5);
        } else {
            STATUS_FANS_LIST.set(0, 0);
        }
    }
}
